<?php
/**
 * MineAdmin is committed to providing solutions for quickly building web applications
 * Please view the LICENSE file that was distributed with this source code,
 * For the full copyright and license information.
 * Thank you very much for using MineAdmin.
 *
 * @Author X.Mo<root@imoi.cn>
 * @Link   https://gitee.com/xmo/MineAdmin
 */

declare (strict_types=1);

namespace Newbie\Abstracts;


use DI\DependencyException;
use DI\NotFoundException;
use Newbie\Cache\Redis;

/**
 * Class AbstractRedis
 * @package Newbie\abstracts
 */
abstract class AbstractRedis {
    /**
     * 缓存前缀
     */
    protected string $prefix = 'newbie';

    /**
     * key 类型名
     */
    protected string $typeName = '';

    /**
     * 获取实例
     * @return mixed
     * @throws DependencyException
     * @throws NotFoundException
     */
    public static function getInstance (): mixed {
        return container()->get(static::class);
    }

    /**
     * 获取redis实例
     * @return Redis
     * @throws DependencyException
     * @throws NotFoundException
     */
    public function redis (): Redis {
        return redis();
    }

    /**
     * 获取key
     * @param string $key
     * @return string|null
     */
    public function getKey (string $key): ?string {
        return $this->prefix . trim($this->typeName, ':') . ':' . $key;
    }

}